package com.nema.servlet;

import com.nema.infer.NemaInfer;
import lsh.LshPackage.Pair;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Servlet implementation class NemaQuery
 */
@WebServlet("/NemaQuery")
public class NemaQuery extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NemaQuery() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Map<String, String> input = new HashMap<String, String>();
        // 接收数据
        String[] attrib = {"score", "timespan", "duration", "price", "sale_effiency", "country", "language", "education", "certification", "level", "avg_resp_time"};
        input.put("gig", request.getParameter("gig"));
        for(int i = 0; i < attrib.length; i++){
        	if( !attrib[i].equals("tag") ){
        		input.put(attrib[i], request.getParameter(attrib[i]));
        	}  	
        }
        String[] arr = request.getParameterValues("tag");
        
        if(arr == null)
        	input.put("tag", "-1");
        else{
        	String temp = "";
            for(String str : arr)
            	temp += ";"+str;
        	input.put("tag", temp.substring(1));
        }

		Pair[] pairs = (Pair[]) request.getSession().getAttribute("pairs");
        NemaInfer nema = new NemaInfer(input,pairs);
		nema.inference();
		String res = nema.printOPT();
        
		request.setAttribute("res",res);      
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("result.jsp");
        requestDispatcher.forward(request,response);
        /*PrintWriter out = response.getWriter();
        out.print("<script>alert('"+country+"');history.go(-1);</script>");
        out.flush();
        out.close();*/
	}

}










